//
//  IcRequestRecommendationViewController.m
//  IceClear
//
//  Created by P & C on 3/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcRequestRecommendationViewController.h"
#import "IcRecommendationDetailViewController.h"


@implementation IcRequestRecommendationViewController


@synthesize inputFieldFacilityType;
@synthesize inputFieldRecType;
@synthesize inputFieldPrecipType;
@synthesize inputFieldTemperature;
@synthesize mainScrollView;
@synthesize appModelsManager;


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	if (self.navigationController) {
		// init nav controls
		UIBarButtonItem* btnCancel = [[[UIBarButtonItem alloc] 
									  initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered 
									  target:self action:@selector(onCancelRequestRecommendationAction:)] autorelease];
		UIBarButtonItem* btnDone = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered 
																	target:self action:@selector(onGetRecommendationAction:)] 
									autorelease];
		
		self.navigationItem.leftBarButtonItem = btnCancel;
		self.navigationItem.rightBarButtonItem = btnDone;
	}
	
	// re-init input fields
	// to be consitent across the entire application
	// we use one of the models managers to grab the
	// different types of facilities, precipitations, ...
	
	int i = 0;
	
	[inputFieldFacilityType removeAllSegments];
	// ask the appropriate models manager for the different facilities
	// supported by the application and use them in the segmented control
	for (NSString * s in self.appModelsManager.recommendationsManager.facilityTypes) {
		[inputFieldFacilityType insertSegmentWithTitle:s atIndex:i animated:NO];
		i++;
	}
	
	i = 0;
	
	[inputFieldPrecipType removeAllSegments];
	// ask the appropriate models manager for the different precipitations
	// supported by the application and use them in the segmented control
	for (NSString * s in self.appModelsManager.recommendationsManager.precipitationTypes) {
		[inputFieldPrecipType insertSegmentWithTitle:s atIndex:i animated:NO];
		i++;
	}
	
	// reset the option that is selected by default in each segmented control
	inputFieldFacilityType.selectedSegmentIndex = 0;
	inputFieldPrecipType.selectedSegmentIndex = 0;
	// all the input fields are wrapped inside a scroll view; we need to
	// set the content size appropriate so that scrolling will be available
	mainScrollView.contentSize = CGSizeMake(320, 570);
	// show the bg
	self.view.backgroundColor = [UIColor clearColor];
}

-(void)onCancelRequestRecommendationAction:(id)sender {
	if (self.navigationController) {
		// go back
		[self.navigationController popViewControllerAnimated:YES];
	}
}

-(void)onGetRecommendationAction:(id)sender {
	NSLog(@"Get Recommmendation");
	
	// get new recommmendation from this set of inputs
	NSUInteger use = inputFieldRecType.selectedSegmentIndex;
	NSUInteger facility = inputFieldFacilityType.selectedSegmentIndex;
	NSUInteger precip = inputFieldPrecipType.selectedSegmentIndex;
	NSUInteger temp = [inputFieldTemperature.text intValue];
	
	// the way to do it is to ask the appropriate models manager by giving
	// him the chosen values by the user
	IcRecommendation* rec = [appModelsManager.recommendationsManager getRecommendationTo:use onFacilityOfType:facility 
																		   atTemperature:temp forPrecipitationOfType:precip];
	
	// show it in a recommendation detail view controller
	// wrapped in a navigation controller and handle
	// hide action here
	IcRecommendationDetailViewController* detailVc = [[IcRecommendationDetailViewController alloc] 
													  initWithStyle:UITableViewStyleGrouped];
	UIBarButtonItem* btnBack = [[[UIBarButtonItem alloc] 
								   initWithTitle:@"Back" style:UIBarButtonItemStyleBordered 
								   target:self action:@selector(onHideRecommendationDetailAction:)] autorelease];
	
	// customize it first and give it references to the models it
	// needs in order to function
	detailVc.title = @"New Recommendation";
	detailVc.navigationItem.leftBarButtonItem = btnBack;
	detailVc.recommendation = rec;
	detailVc.appModelsManager = appModelsManager;
	
	// wrap it in a navigation controller
	UINavigationController* tmpNavCont = [[UINavigationController alloc] initWithRootViewController:detailVc];
	
	tmpNavCont.view.backgroundColor = [UIColor clearColor];
	
	// show it
	[self presentModalViewController:tmpNavCont animated:YES];
	
	[detailVc release];
	[tmpNavCont release];
	[rec release];
}

-(void)onHideRecommendationDetailAction:(id)sender {
	[self dismissModalViewControllerAnimated:YES];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[inputFieldRecType release];
	[inputFieldTemperature release];
	[inputFieldPrecipType release];
	[inputFieldFacilityType release];
	[mainScrollView release];
	[appModelsManager release];
    [super dealloc];
}


@end
